package com.rbac.application.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.rbac.application.service.OperationLogService;
import com.rbac.application.service.ProfileService;
import com.rbac.application.service.UserService;
import com.rbac.common.enums.BusinessErrorCode;
import com.rbac.common.exception.BusinessException;
import com.rbac.domain.dto.request.PasswordChangeRequest;
import com.rbac.domain.dto.request.ProfileUpdateRequest;
import com.rbac.domain.dto.response.UserInfoResponse;
import com.rbac.domain.entity.OperationLog;
import com.rbac.domain.entity.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ProfileServiceImpl implements ProfileService {

    private final UserService userService;
    private final OperationLogService operationLogService;

    @Override
    public UserInfoResponse getCurrentUserProfile() {
        Long userId = StpUtil.getLoginIdAsLong();
        User user = userService.getById(userId);
        
        if (user == null) {
            throw new BusinessException(BusinessErrorCode.USER_NOT_FOUND);
        }

        UserInfoResponse userInfo = new UserInfoResponse();
        userInfo.setId(user.getId());
        userInfo.setUsername(user.getUsername());
        userInfo.setNickname(user.getNickname());
        userInfo.setEmail(user.getEmail());
        userInfo.setPhone(user.getPhone());
        userInfo.setAvatar(user.getAvatar());
        userInfo.setStatus(user.getStatus());
        userInfo.setLoginTime(user.getLoginTime());

        return userInfo;
    }

    @Override
    public UserInfoResponse updateProfile(ProfileUpdateRequest request) {
        Long userId = StpUtil.getLoginIdAsLong();
        User user = userService.getById(userId);
        
        if (user == null) {
            throw new BusinessException(BusinessErrorCode.USER_NOT_FOUND);
        }

        user.setNickname(request.getNickname());
        user.setEmail(request.getEmail());
        user.setPhone(request.getPhone());
        user.setAvatar(request.getAvatar());
        user.setUpdatedTime(LocalDateTime.now());

        userService.updateById(user);

        return getCurrentUserProfile();
    }

    @Override
    public void changePassword(PasswordChangeRequest request) {
        if (!request.getNewPassword().equals(request.getConfirmPassword())) {
            throw new BusinessException(BusinessErrorCode.PASSWORD_MISMATCH);
        }

        Long userId = StpUtil.getLoginIdAsLong();
        User user = userService.getById(userId);
        
        if (user == null) {
            throw new BusinessException(BusinessErrorCode.USER_NOT_FOUND);
        }

        // 验证当前密码
        if (!BCrypt.checkpw(request.getOldPassword(), user.getPassword())) {
            throw new BusinessException(BusinessErrorCode.OLD_PASSWORD_INCORRECT);
        }

        // 更新密码
        user.setPassword(BCrypt.hashpw(request.getNewPassword()));
        user.setUpdatedTime(LocalDateTime.now());
        userService.updateById(user);
    }

    @Override
    public List<OperationLog> getMyOperationLogs(int limit) {
        Long userId = StpUtil.getLoginIdAsLong();
        List<OperationLog> logs = operationLogService.getLogsByUserId(userId);
        
        // 限制返回数量
        if (logs.size() > limit) {
            return logs.subList(0, limit);
        }
        
        return logs;
    }
}